- Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy path2166. Design Bitset.go
72 lines (62 loc) · 1.29 KB
/
2166. Design Bitset.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package leetcode
typeBitsetstruct {
set []byte
flipped []byte
oneCountint
sizeint
}
funcConstructor(sizeint) Bitset {
set:=make([]byte, size)
flipped:=make([]byte, size)
fori:=0; i<size; i++ {
set[i] =byte('0')
flipped[i] =byte('1')
}
returnBitset{
set: set,
flipped: flipped,
oneCount: 0,
size: size,
}
}
func (this*Bitset) Fix(idxint) {
ifthis.set[idx] ==byte('0') {
this.set[idx] =byte('1')
this.flipped[idx] =byte('0')
this.oneCount++
}
}
func (this*Bitset) Unfix(idxint) {
ifthis.set[idx] ==byte('1') {
this.set[idx] =byte('0')
this.flipped[idx] =byte('1')
this.oneCount--
}
}
func (this*Bitset) Flip() {
this.set, this.flipped=this.flipped, this.set
this.oneCount=this.size-this.oneCount
}
func (this*Bitset) All() bool {
returnthis.oneCount==this.size
}
func (this*Bitset) One() bool {
returnthis.oneCount!=0
}
func (this*Bitset) Count() int {
returnthis.oneCount
}
func (this*Bitset) ToString() string {
returnstring(this.set)
}
/**
* Your Bitset object will be instantiated and called as such:
* obj := Constructor(size);
* obj.Fix(idx);
* obj.Unfix(idx);
* obj.Flip();
* param_4 := obj.All();
* param_5 := obj.One();
* param_6 := obj.Count();
* param_7 := obj.ToString();
*/